﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Elca.MvvmHelpers;

namespace ULSDeobfuscator {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : IGetSelection {
        public MainWindow() {
            InitializeComponent();

            // An exception to MVVM. It works fine to set these bindings in XAML, but the designer insists on showing a window of size 0,0
            Binding bWidth = new Binding("WindowWidth") {
                    Mode = BindingMode.TwoWay
            };
            SetBinding(WidthProperty, bWidth);

            Binding bHeight = new Binding("WindowHeight") {
                    Mode = BindingMode.TwoWay
            };
            SetBinding(HeightProperty, bHeight);

            ((MainWindowViewModel) DataContext).GetSelection = this;
        }

        protected override void AddSystemMenus() {
            
            SystemMenuItem aboutItem = new SystemMenuItem {
                    Header = "About",
                    Id = 100,
                    Command = ((MainWindowViewModel) DataContext).AboutCommand,
            };
            MenuItems.Add(aboutItem);
        }

        // I need the mouse coordinates for these 3 events, so they are not MVVM friendly

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            Grid g = (Grid)sender;
            Point pt = e.MouseDevice.GetPosition(g);
            e.MouseDevice.Capture(g);
            ((MainWindowViewModel)DataContext).StartDrag(pt.X);
        }

        private void OnMouseMove(object sender, MouseEventArgs e) {

            Grid g = (Grid)sender;
            Point pt = e.MouseDevice.GetPosition(g);
            ((MainWindowViewModel)DataContext).Drag(pt.X);
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {

            Grid g = (Grid)sender;

            Point pt = e.MouseDevice.GetPosition(g);

            ((MainWindowViewModel)DataContext).StopDrag(pt.X);
            e.MouseDevice.Capture(null);
        }

        public IList SelectedItems {
            get { return _lvLog.SelectedItems; }
        }

        private void OnListViewContextMenuOpening(object sender, ContextMenuEventArgs e) {
            // we need to set the data context of the context menu back to the 'main' data context (instead of the log entry) in order to 
            // be able to define the context menu commands at the level of MainWindowViewModel instead of defining the commands in each log entry for memory consumption reasons
            // in WPF4, this could be done in XAML through the {x:Reference _lvlLog} syntax
            MainWindowViewModel mwvm = (MainWindowViewModel)DataContext;
            FrameworkElement fe = (FrameworkElement)sender;
            ContextMenu cm = fe.ContextMenu;
            cm.DataContext = mwvm;
        }
    }
}
